<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5                                                          |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License,        |
// | that is available at http://www.php.net/license/3_0.txt.               |
// | If you did not receive a copy of the PHP license and are unable to     |
// | obtain it through the world-wide-web, please send a note to            |
// | license@php.net so we can mail you a copy immediately.                 |
// +------------------------------------------------------------------------+
// | Author:                                                                |
// +------------------------------------------------------------------------+
//
// $Id$
//


/**
 * @author       mk
 */
class PdoSession extends Session {
    
    /**
     * @var      PDO
     */
    protected $db;
    protected $session_table = 'session';
    
    protected function initialize() {
        $this->db = ConnectionManager::getConn()->getConnection();
        session_set_save_handler(array($this, 'sessionOpen'),
                                 array($this, 'sessionClose'),
                                 array($this, 'sessionRead'),
                                 array($this, 'sessionWrite'),
                                 array($this, 'sessionDestroy'),
                                 array($this, 'sessionGC'));
        session_start();
    }
    /**
     * @return   Session
     */
    static public function getInstance() {
        if (!isset(self::$instance)) {
            $class = __CLASS__;
            self::$instance = new $class();
            self::$instance->initialize();
        }
        
        return self::$instance;
    }
    
    /**
     * @return   boolean
     */
    public function sessionOpen($path, $name) {
        return true;
    }
    
    /**
     * @return   boolean
     */
    public function sessionClose() {
        return true;
    }
    
    /**
     * @param    string $id    
     * @return   array
     */
    public function sessionRead($id) {
        $sql = "SELECT sess_data FROM {$this->session_table} WHERE id = ?";
        $stmt = $this->db->prepare($sql);
        //$stmt->bindParam(1, $id, PDO::PARAM_STR, 255);
        $stmt->execute(array($id));
        
        if ($data = $stmt->fetchColumn()) {
            return $data;
        } else {
            $sql = "INSERT {$this->session_table}(sess_id, sess_data, sess_time) VALUES(?, ?, ?)";
            $stmt = $this->db->prepare($sql);
//            $stmt->bindParam(1, $id, PDO::PARAM_STR);
//            $stmt->bindValue(2, '', PDO::PARAM_STR);
//            $stmt->bindValue(3, time(), PDO::PARAM_INT);
            $stmt->execute(array($id, '', time()));
            
            return '';
        }
    }
    
    /**
     * @param    string $id    
     * @param    string $data    
     * @return   boolean
     */
    public function sessionWrite($id, $data) {
        $sql = "UPDATE {$this->session_table} SET sess_data = ?, sess_time = ? WHERE sess_id = ?";
        
        $stmt = $this->db->prepare($sql);
//        $stmt->bindParam(1, $data, PDO::PARAM_STR);
//        $stmt->bindValue(2, time(), PDO::PARAM_INT);
//        $stmt->bindParam(3, $id, PDO::PARAM_STR);
        $stmt->execute(array($data, time(), $id));
    }
    
    /**
     * @param    string $id    
     * @return   boolean
     */
    public function sessionDestroy($id) {
        $sql = "DELETE FROM {$this->session_table} WHERE sess_id = ?";
        $stmt = $this->db->prepare($sql);
//        $stmt->bindParam(1, $id, PDO::PARAM_STR);
        $stmt->execute(array($id));
    }
    
    /**
     * @return   boolean
     */
    public function sessionGC($lefttime) {
        $sql = "DELETE FROM {$this->session_table} WHERE sess_time < ".(time() - $lefttime);
        $this->db->exec($sql);
        return true;
    }
}

